<h2 style='text-align:center'>商品分类管理</h2>


<!--添加分类按钮区-->
<div id='MainGoodsType_AddTypeArea' style='display:none'>
	<select id="MainGoodsType_AddType">
		<option value="1">添加一级分类</option>
		<option value="2">添加二级分类</option>
		<option value="3">添加三级分类</option>
	</select>

	<div>
		<select id="MainGoodsType_TypeList1" style='display:none'>
			<option value="">一级分类名</option>
		</select>
		<select id="MainGoodsType_TypeList2" style='display:none'>
			<option value="">二级分类名</option>
		</select>
	</div>
	<div>
		<input placeholder='分类名' id='MainGoodsType_AddInpName' />
		<input placeholder='序号' id='MainGoodsType_AddInpOrder'/>
		<button id='MainGoodsType_AddBtn'>添加</button>
		<button id='MainGoodsType_BackBtn'>返回</button>　
	</div>
</div>
<button id='MainGoodsType_AddTypeAreaBtn'>添加分类</button>　

<!--分类显示区-->
<div id='MainGoodsType_ViewGoodsTypeArea'>
	<!--div style='display:flex'>
		<div style='flex:1'>
			<span>男装</span>　
			<span style='font-size:12px'>(2)</span>
		</div>
		<div style='flex:1'><button>删除</button></div>
	</div>
	<div style='margin-left:50px;display:flex'>
		<div style='flex:1'>
			<span>男装</span>　
			<span style='font-size:12px'>(2)</span>
		</div>
		<div style='flex:1;text-align:center'><button>删除</button></div>
	</div-->

</div>



<script>

$('#MainGoodsType_AddTypeAreaBtn').click(function(){
	$('#MainGoodsType_AddTypeArea').show();
	$('#MainGoodsType_AddTypeAreaBtn').hide();
});
$('#MainGoodsType_BackBtn').click(function(){
	$('#MainGoodsType_AddTypeArea').hide();
	$('#MainGoodsType_AddTypeAreaBtn').show();
	$('#MainGoodsType_AddInpName,#MainGoodsType_AddInpOrder').val('');
});


var s1 = new Array();	//装载一级分类
var s2 = new Array();	//装载二级分类
var s3 = new Array();	//装载三级分类

var type_level = null;
var Province_id = null;
var City_id = null;
var County_id = null;

_getGoodsTypeListFull();	//获取商品分类列表

//获取商品分类列表
function _getGoodsTypeListFull(){
	var data = {
		type:'_getGoodsTypeListFull',
	}
	AjaxAdmin(data,function(res){
		var tmp = _checkJsonFormat(res);
		_getGoodsTypeListFullParse(tmp.data);
	})
}




function _getGoodsTypeListFullParse(d){
	if(typeof(d) == 'object' && d.length > 0){
		//将分类分载到各自的集合中
		for(var i=0;i<d.length;i++){
			if(d[i].level == 1){
				s1.push(d[i]);
			}else if(d[i].level == 2){
				s2.push(d[i]);
			}else{
				s3.push(d[i]);
			}
		}

		//将三级分类装载到二级分类的子对象中
		for(var i=0;i<s2.length;i++){
			var tmp = new Array();
			for(var j=0;j<s3.length;j++){
				if(s2[i].id == s3[j].parent_id){
					tmp.push(s3[j]);
				}
			}
			s2[i].s3 = tmp;
		}


		//将二级分类装载到一级分类的子对象中
		for(var i=0;i<s1.length;i++){
			var tmp = new Array();
			for(var j=0;j<s2.length;j++){
				if(s1[i].id == s2[j].parent_id){
					tmp.push(s2[j]);
				}
			}
			s1[i].s2 = tmp;
		}

		var str = '';
		for(var i=0;i<s1.length;i++){
			//一级
			str += "<div style='display:flex;margin:20px auto'>";
			str += "	<div style='flex:1'>";
			str += "		<span>"+s1[i].name+"</span>　";
			str += "		<span style='font-size:12px'>("+s1[i].orders+")</span>";
			str += "	</div>";
			str += "	<div style='flex:2'><input placeholder='改序改名' size=6 style='display:none' /><button onclick=MainGoodsType_ChangeOrder(this,"+s1[i].id+")>改序</button><button onclick=MainGoodsType_ChangeName(this,"+s1[i].id+")>改名</button><button onclick=MainGoodsType_Del1("+s1[i].id+")>删除</button><button onclick=MainGoodsType_SubShow(this)>收起</button></div>";
			str += "</div>";

			//二级
			var s2_tmp = s1[i].s2;
			str += "<div>";
			for(var j=0;j<s2_tmp.length;j++){
				str += "<div style='margin-left:50px;display:flex'>";
				str += "	<div style='flex:1'>";
				str += "		<span>"+s2_tmp[j].name+"</span>　";
				str += "		<span style='font-size:12px'>("+s2_tmp[j].orders+")</span>";
				str += "	</div>";
				str += "	<div style='flex:2;text-align:center'><input placeholder='改序改名' size=6 style='display:none'/><button onclick=MainGoodsType_ChangeOrder(this,"+s2_tmp[j].id+")>改序</button><button onclick=MainGoodsType_ChangeName(this,"+s2_tmp[j].id+")>改名</button><button onclick=MainGoodsType_Del1("+s2_tmp[j].id+")>删除</button><button onclick=MainGoodsType_SubShow(this)>收起</button></div>";
				str += "</div>";

				//三级
				var s3_tmp = s2_tmp[j].s3;
				str += "<div>";
				for(var k=0;k<s3_tmp.length;k++){
					str += "<div style='margin-left:100px;display:flex'>";
					str += "	<div style='flex:1'>";
					str += "		<span>"+s3_tmp[k].name+"</span>　";
					str += "		<span style='font-size:12px'>("+s3_tmp[k].orders+")</span>";
					str += "	</div>";
					str += "	<div style='flex:1;text-align:right'><input placeholder='改序改名' size=6 style='display:none' /><button onclick=MainGoodsType_ChangeOrder(this,"+s3_tmp[k].id+")>改序</button><button onclick=MainGoodsType_ChangeName(this,"+s3_tmp[k].id+")>改名</button><button onclick=MainGoodsType_Del1("+s3_tmp[k].id+")>删除</button></div>";
					str += "</div>";
				}
				str += "</div>";
			}
			str += "</div>";

		}
		$('#MainGoodsType_ViewGoodsTypeArea').html(str);
	}else{
		$('#MainGoodsType_ViewGoodsTypeArea').html('');
	}

	//TypeAddCon();
}




// ### 添加类别
//初始化选择级别
var a = $('#MainGoodsType_AddType').find('option:selected').val();	//分类级别选择值
if(!type_level){
	type_level = a;
}
$('#MainGoodsType_AddType').change(function(){
	var a = $('#MainGoodsType_AddType').find('option:selected').val();	//分类级别选择值
	type_level = a;
	MainGoodsType_ViewSelTo12();	//根据选择的分类级别，相应显示一二级的选择框

});
MainGoodsType_ViewSelTo12();	//根据选择的分类级别，相应显示一二级的选择框

//点击添加按钮
$('#MainGoodsType_AddBtn').click(function(){
	var name = ChangeChar($('#MainGoodsType_AddInpName').val().trim());
	var order = ChangeChar($('#MainGoodsType_AddInpOrder').val().trim());

	//Field_Check(/^.+$/,name,'js类名非法！');
	//Field_Check(/^[0-9]{1,3}$/,order,'js序号非法！');

	switch(type_level){
		case 1:
		case '1':
			var level = 1;
			var parent_id = 0;
			break;
		case 2:
		case '2':
			var level = 2;
			var parent_id = $('#MainGoodsType_TypeList1').find('option:selected').val();
			break;
		case 3:
		case '3':
			var level = 3;
			var parent_id = $('#MainGoodsType_TypeList2').find('option:selected').val();
			break;
	}
	var data = {
		type:'goodsTypeAdd',
		name:name,
		order:order,
		level:level,
		parent_id:parent_id
	};
	AjaxAdmin(data,function(res){
		var tmp = _checkJsonFormat(res);
		switch(tmp.stat){
			case 'err3':
				A('添加失败!');
				break;
			case 'err2':
				A('分类已存在!');
				break;
			case 'ok':
				A('添加成功!');
				_flushData();	//刷新数据
				break;
		}
	});
});




//刷新数据
function _flushData(){
	$('#MainGoodsType_ViewGoodsTypeArea').html('');
	s1 = new Array();	//装载一级分类
	s2 = new Array();	//装载二级分类
	s3 = new Array();	//装载三级分类
	_getGoodsTypeListFull();	//获取商品分类列表
}



//根据选择的分类级别，相应显示一二级的选择框 
function MainGoodsType_ViewSelTo12(){
	switch(type_level){
		case 1:
		case '1':
			$('#MainGoodsType_TypeList1').hide();
			$('#MainGoodsType_TypeList2').hide();
			break;
		case 2:
		case '2':
			
			MainGoodsType_TypeList1Str();	//获取一级分类的option字串并显示
			$('#MainGoodsType_TypeList1').show();
			$('#MainGoodsType_TypeList2').hide();
			break;
		case 3:
		case '3':
			MainGoodsType_TypeList1Str();	//获取一级分类的option字串并显示

			MainGoodsType_TypeList1Str2();	//获取二级分类的option字串并显示


			$('#MainGoodsType_TypeList1').change(function(){
				City_id = null;
				MainGoodsType_TypeList1Str2();	//获取二级分类的option字串并显示
			});


			$('#MainGoodsType_TypeList1').show();
			$('#MainGoodsType_TypeList2').show();
			break;
	}
}

//获取一级分类的option字串并显示
function MainGoodsType_TypeList1Str(){
	var tmp = '';
	for(var i=0;i<s1.length;i++){
		tmp += "<option value='"+s1[i].id+"''>"+s1[i].name+"</option>";
	}
	$('#MainGoodsType_TypeList1').html(tmp);
	if(!Province_id){
		Province_id = $('#MainGoodsType_TypeList1').find('option:selected').val();
	}else{
		$('#MainGoodsType_TypeList1').val(Province_id);
	}
	$('#MainGoodsType_TypeList1').change(function(){
		Province_id = $('#MainGoodsType_TypeList1').find('option:selected').val();
	});
}
//获取二级分类的option字串并显示
function  MainGoodsType_TypeList1Str2(){
	//var Province_id = $('#MainGoodsType_TypeList1').find('option:selected').val();
	var s2_tmp = '';
	for(var i=0;i<s1.length;i++){
		if(Province_id == s1[i].id){
			s2_tmp = s1[i].s2;
			break;
		}
	}
	var s2_tmp_str = '';
	for(var i=0;i<s2_tmp.length;i++){
		s2_tmp_str += "<option value='"+s2_tmp[i].id+"''>"+s2_tmp[i].name+"</option>";
	}
	$('#MainGoodsType_TypeList2').html(s2_tmp_str);
	//C($('#MainGoodsType_TypeList2').find('option:selected').val());
	if(!City_id){
		City_id = $('#MainGoodsType_TypeList2').find('option:selected').val();
	}else{
		$('#MainGoodsType_TypeList2').val(City_id);
	}
	$('#MainGoodsType_TypeList2').change(function(){
		City_id = $('#MainGoodsType_TypeList2').find('option:selected').val();
		//C(City_id);
	});
}



//改名
function MainGoodsType_ChangeName(o,id){
	if($(o).prev().prev('input').is(':hidden')){
		$(o).prev().prev('input').show();
	}else{
		var name = $(o).prev().prev('input').val().trim();
		if(!name){
			$(o).prev().prev('input').hide();
			return;
		}
		if(/^[0-9]|[!@#$%^&*()_+=\\\-\[\]\{\}\,\.\<\`\~>;:'"|\/\?]/.test(name)){
			A('名字非法！');return;
		}
		var data = {
			type:'goodsTypeChangeName',
			id:id,
			name:name,
		};
		AjaxAdmin(data,function(res){
			var tmp = _checkJsonFormat(res);
			if(tmp.stat == 'ok'){
				A('操作成功！');
				_flushData();	//刷新数据
			}else{
				A('操作失败！');
			}
		});
	}
}



//改序
function MainGoodsType_ChangeOrder(o,id){
	if($(o).prev('input').is(':hidden')){
		$(o).prev('input').show();
	}else{
		var order = $(o).prev('input').val().trim();
		if(!order){
			$(o).prev('input').hide();
			return;
		}
		if(!/^[1-9][0-9]*$/.test(order)){
			A('序号必须是整数！');return;
		}
		var data = {
			type:'goodsTypeChangeOrder',
			id:id,
			order:order,
		};
		AjaxAdmin(data,function(res){
			var tmp = _checkJsonFormat(res);
			if(tmp.stat == 'ok'){
				A('操作成功！');
				_flushData();	//刷新数据
			}else{
				A('操作失败！');
			}
		});
	}
}


//删除类别
function MainGoodsType_Del1(id){
	if(!id){
		return;
	}
	if(!_confirm()){
		return;
	}
	var data = {
		type:'goodsTypeDel',
		id:id
	};
	AjaxAdmin(data,function(res){
		var tmp = _checkJsonFormat(res);
		if(tmp.stat == 'ok'){
			A('删除成功！');
			_flushData();	//刷新数据
		}else{
			A('操作失败！');
		}
	});
}


//展示子类别列表
function MainGoodsType_SubShow(o){
	var ele = $(o).parent().parent().next('div');
	if(ele.is(":hidden")){
		ele.show();
		$(o).text('收起');
	}else{
		ele.hide();
		$(o).text('展示');
	}
}

</script>